package com.platform.apex.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * common object convert tools
 *
 * @author Jax
 */
public class ConvertUtil {
	static {
        int features = 0;
        features |= SerializerFeature.QuoteFieldNames.getMask();
        features |= SerializerFeature.SkipTransientField.getMask();
        features |= SerializerFeature.DisableCircularReferenceDetect.getMask();
        features |= SerializerFeature.WriteEnumUsingToString.getMask();
        features |= SerializerFeature.WriteMapNullValue.getMask();
        
		JSON.DEFAULT_GENERATE_FEATURE = features;
	}

    private final static ObjectMapper OBJECT_MAPPER = ObjectMapperUtil.getObjectMapperForRouting();

    public static <T> List<T> mapListToObjectList(List<Map<String, Object>> mapList, Class<T> beanClass) {
        List<T> listResult = new ArrayList<T>();

        if (mapList != null && mapList.size() < 1) {
            return listResult;
        }
        if (mapList != null) {
            for (Map<String, Object> eachMap : mapList) {
                T bean = mapToObject(eachMap, beanClass);
                if (bean == null) continue;
                listResult.add(bean);
            }
        }
        return listResult;
    }

    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
        return objectToObj(map, beanClass);
    }

    public static <T> T jsonStrToObject(String jsonStr, Class<T> beanClass) {
        try {
            if (ObjUtil.isEmpty(jsonStr)) return null;

            return OBJECT_MAPPER.readValue(jsonStr, beanClass);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static <T> T objectToObj(Object obj, Class<T> beanClass) {
        if (obj == null) return null;

        if (obj instanceof String) return jsonStrToObject(obj.toString(), beanClass);

        return jsonStrToObject(objectToStr(obj), beanClass);
    }

    public static String objectToStr(Object obj) {
        try {
            if (obj == null) return null;

            return OBJECT_MAPPER.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static <T> List<T> jsonStrToList(String jsonStr, Class<T> beanClass) {
		try {
			if(ObjUtil.isEmpty(jsonStr)) return null;

			return JSON.parseArray(jsonStr, beanClass);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    public static <K, V> Map<K, V> jsonStrToMap(String jsonStr, Map<K, V> map) {
		try {
			if(ObjUtil.isEmpty(jsonStr)) return null;

			return JSON.parseObject(jsonStr, new TypeReference<Map<K, V>>() {});
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    
    public static <T> T jsonStrToObjectAli(String jsonStr, Class<T> beanClass) {
		try {
			if(ObjUtil.isEmpty(jsonStr)) return null;

			return JSON.parseObject(jsonStr, beanClass);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
